package github.sf.fw.model;

import java.util.Collections;

public class RS<T> {
    /***
     * 信息
     */
    private final String msg;
    /***
     * 响应码
     */
    private final Integer code;
    /***
     * 数据
     */
    private T data;

    public RS(String msg, Integer code, T dataValue) {
        this.msg = msg;
        this.code = code;
        if (dataValue == null) {
            this.data = (T) Collections.emptyList();
        }

        this.data = dataValue;
    }

    public RS(String msg, Integer code) {
        this.msg = msg;
        this.code = code;
        this.data = (T) Collections.emptyList();
    }

    public RS(ResultStatus resultStatus, T data) {
        this.msg = resultStatus.getMessage();
        this.code = resultStatus.getResultCode();
        this.data = data;
    }

    public static <T> RS<T> success() {
        return new RS(ResultStatus.OK, Collections.EMPTY_LIST);
    }

    public static <T> RS<T> success(T data) {
        return new RS<>(ResultStatus.OK, data);
    }

    public static <T> RS<T> success(String msg) {
        return new RS<>(msg, ResultStatus.OK.getResultCode());
    }

    public static <T> RS<T> success(String msg, T data) {
        return new RS<>(msg, ResultStatus.OK.getResultCode(), data);
    }

    public static <T> RS<T> success(Integer code, String msg, T data) {
        return new RS<>(msg, code, data);
    }

    public static <T> RS<T> fail() {
        return new RS(ResultStatus.FAIL, Collections.EMPTY_LIST);
    }

    public static <T> RS<T> fail(T data) {
        return new RS<>(ResultStatus.FAIL, data);
    }

    public static <T> RS<T> fail(String msg) {
        return new RS<>(msg, ResultStatus.FAIL.getResultCode());
    }

    public static <T> RS<T> fail(String msg, T data) {
        return new RS<>(msg, ResultStatus.FAIL.getResultCode(), data);
    }

    public static <T> RS<T> fail(Integer code, String msg, T data) {
        return new RS<>(msg, code, data);
    }

    public String getMsg() {
        return msg;
    }

    public Integer getCode() {
        return code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public enum ResultStatus {
        OK("success", 200),
        FAIL("failed", -1);

        private final String message;
        private final int resultCode;

        ResultStatus(String message, int resultCode) {
            this.message = message;
            this.resultCode = resultCode;
        }

        public String getMessage() {
            return this.message;
        }

        public int getResultCode() {
            return this.resultCode;
        }
    }
}
